import pageJson from "@/pages.json";

/**
 * @description 将 page.json 中定义的页面配置转换为对象，包括主包和分包
 * @returns {object} 转换后的页面配置对象
 */
export default function getPages() {
	const pages = [];

	// 主包
	const mainPages = pageJson?.pages || [];

	// 处理一下，加一个全路径字段
	mainPages.forEach((page) => {
		let path = page?.path || "";
		if (!path?.startsWith?.("/")) path = `/${path}`;
		page.fullPath = path;
	});

	pages.push(...mainPages);

	// 分包
	const subPackages = pageJson?.subPackages || [];
	subPackages.forEach((subPackage) => {
		let root = subPackage?.root || "";
		const subPages = subPackage?.pages || [];

		// 将 root 处理成 以斜杠开头和结尾的格式
		if (!root?.startsWith?.("/")) root = "/" + root;
		if (!root.endsWith("/")) root = root + "/";

		// 处理一下，加一个全路径字段
		subPages.forEach((page) => {
			let path = page?.path || "";
			if (path?.startsWith?.("/")) {
				// 删掉开头的斜杠
				path = path.slice(1);
				page.fullPath = root + path;
			} else {
				page.fullPath = root + path;
			}
		});

		pages.push(...subPages);
	});

	return pages;
}
